package com.ghy.controller;

import com.ghy.constant.ML;
import com.ghy.dto.UserInsertDTO;
import com.ghy.dto.UserPageDTO;
import com.ghy.dto.UserUpdateDTO;
import com.ghy.dto.UserUpdatePasswordDTO;
import com.ghy.entity.User;
import com.ghy.excel.UserExcelDTO;
import com.ghy.result.Result;
import com.ghy.util.EasyExcelUtil;
import com.ghy.vo.LoginVO;
import com.ghy.vo.RoleSimpleListVO;
import com.ghy.vo.UserSimpleListVO;
import com.mybatisflex.core.paginate.Page;
import com.ghy.dto.*;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.beans.factory.annotation.Autowired;
import com.ghy.service.UserService;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.Map;

/**
 * 用户表 控制层。
 *
 * @author ghy
 * @since 2024-12-22
 */
@Tag(name = "用户")
@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    private UserService userService;

    @Operation(summary ="新增——单条",description ="新增一条用户记录")
    @PostMapping("insert")
    public boolean insert(@Validated @RequestBody UserInsertDTO dto) {
        return userService.insert(dto);
    }

    @Operation(summary ="查询——单条",description ="按主键查询一条用户记录")
    @GetMapping("select/{id}")
    public User select(@PathVariable("id") Long id) {
        return userService.select(id);
    }

    @Operation(summary = "查询——简单查询全部", description = "查询全部用户记录")
    @GetMapping("simpleList")
    public List<UserSimpleListVO> simpleList() {
        return userService.simpleList();
    }

    @Operation(summary ="查询——分页",description ="分页查询用户记录")
    @GetMapping("page")
    public Page<User> page(@Validated UserPageDTO dto) {
        return userService.page(dto);
    }

    @Operation(summary="修改——单条",description="按主键修改一条用户记录")
    @PutMapping("update")
    public boolean update(@RequestBody UserUpdateDTO dto) {
        return userService.update(dto);
    }

    @Operation(summary ="删除——单条",description ="按主键删除一条用户记录")
    @DeleteMapping("delete/{id}")
    public boolean delete(@PathVariable("id") Long id) {
        return userService.delete(id);
    }

    @Operation(summary="删除——批量",description ="按主键批量删除用户记录")
    @DeleteMapping("deleteBatch")
    public boolean deleteBatch(@RequestParam("ids") List<Long> ids) {
        return userService.deleteBatch(ids);
    }

    @Operation(summary ="查询——角色ID列表",description="按主键查询用户的全部角色ID列表")
    @GetMapping("listRoleIds/{id}")
    public List<Long> listMenuIds(@PathVariable("id") Long id) {
        return userService.listRoleIds(id);
    }

    @Operation(summary="修改——角色列表",description="按主键修改用户的角色列表")
    @PutMapping("updateRoles")
    public  boolean updateMenus(@RequestBody UserUpdateDTO dto){
        return userService.updateRoles(dto);
    }

    @Operation(summary ="修改——重置密码",description ="按主键重置用户的登录密码为默认密码")
    @PutMapping("resetPassword/{id}")
    public boolean resetPassword(@PathVariable("id") Long id) {
        return userService.resetPassword(id) ;
    }

    @Operation(summary ="修改——密码",description ="")
    @PutMapping("updatePassword")
    public boolean updatePassword(@RequestBody UserUpdatePasswordDTO dto){
        return userService.updatePassword(dto);
    }

    @Operation(summary="查询——统计数据",description ="查询用户相关的统计数据")
    @GetMapping("statistics")
    public Map<String, Object> statistics() {
        return userService.statistics();
    }

    @Operation(summary="查询——报表打印",description ="打印用户相关的报表数据")
    @GetMapping("excel")
    public void excel(HttpServletResponse response) {
        EasyExcelUtil.download(response,
                ML.User.EXCEL_FILE_NAME,
                ML.User.EXCEL_SHEET_NAME,
                UserExcelDTO.class,
                userService.excel());
    }

    @Operation(summary = "查询——验证码", description = "获取手机号码的验证码")
    @GetMapping("getVcode/{phone}")
    public Result<String> getVcode(@PathVariable("phone") String phone) {
        return new Result<>(userService.getVcode(phone));
    }

    @Operation(summary = "登录 - 账号密码登录", description = "按账号和密码登录系统")
    @PostMapping("loginByAccount")
    public LoginVO loginByAccount(@Validated @RequestBody LoginByAccountDTO dto) {
        return userService.loginByAccount(dto);
    }

    @Operation(summary = "登录 - 手机号登录", description = "按手机号验证码登录系统")
    @PostMapping("loginByPhone")
    public LoginVO loginByPhone(@Validated @RequestBody LoginByPhoneDTO dto){
        return userService.loginByPhone(dto);
    }

    @Operation(summary = "修改 - 用户头像", description = "按主键修改用户的头像")
    @PostMapping("/uploadAvatar/{id}")
    public Result<String> uploadAvatar(@PathVariable("id") Long id, @RequestParam("avatarFile") MultipartFile avatarFile) {
        return new Result<>(userService.uploadAvatar(avatarFile, id));
    }

}
